/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.search.strategy.model;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.groupme.model.Identifiable;

/**
 * created on Jul 6, 2007
 * 
 * This class defines a simple abstract structure for assigning weights to identifiables.
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.6 $ $Date: 2008-11-17 15:43:45 $
 */
public abstract class AbstractWeightedIdentifiable {
	
	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	/** the group that is assigned with a weight */
	private Identifiable identifiable;
	
	/** the weight that is associated to the group */
	public double weight;
	

	/**
	 * This method returns the identifiable which is wrapped by the AbstractWeightedIdentifiable object.
	 * @return the identifiable
	 */
	public Identifiable getIdentifiable() {
		return identifiable;
	}

	/**
	 * This method returns the weight of an AbstractWeightedIdentifiable object.
	 * @return the weight
	 */
	public double getWeight() {
		return weight;
	}
	
	/**
	 * This method sets the weight of a AbstractWeightedIdentifiable object.
	 * @param weight the weight to set
	 */
	public void setWeight(double weight) {
		this.weight = weight;
	}

	/**
	 * This equals-method just compares the URIs of the two identifiables.
	 * @param other the other {@link AbstractWeightedIdentifiable} to compare with
	 * @return returns <code>true</code> if the URIs of the identifiables (of this and the given
	 * {@link AbstractWeightedIdentifiable} instances) are equal.
	 */
	@Override
	public boolean equals(Object other) {
		if(other instanceof AbstractWeightedIdentifiable){
			return this.getIdentifiable().getId().equals(((AbstractWeightedIdentifiable)other).getIdentifiable().getId());
		}else{
			return false;
		}
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		if(this.getIdentifiable() != null){
			return this.getIdentifiable().getLabel() + "[uri: " + this.getIdentifiable().getURI() +"]" + " (" + this.getWeight() + ")";
		}
		return super.toString();
	}
	
	/**
	 * Clones an {@link AbstractWeightedIdentifiable}, i.e. returns an
	 * {@link AbstractWeightedIdentifiable} object with same resource and same 
	 * weight as a new reference.
	 */
	public abstract AbstractWeightedIdentifiable clone();
}
